package com.ruoyi.odorservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.odorservice.entity.Site;
import com.ruoyi.odorservice.mapper.SiteMapper;
import com.ruoyi.odorservice.model.vo.SiteInfoVo;
import com.ruoyi.odorservice.model.vo.SiteNameVo;
import com.ruoyi.odorservice.service.SiteService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements SiteService {
    @Override
    public List<SiteInfoVo> getSiteInfoList(String siteName, Integer companyId) {
        return this.baseMapper.getSiteInfoList(siteName, companyId);
    }

    @Override
    public Boolean createSite(Site site) {
        return save(site);
    }

    @Override
    public Boolean updateSite(Site site) {
        if (StringUtils.isNull(site) || site.getId() == null) {
            return false;
        }
        return updateById(site);
    }

    @Override
    public Boolean deleteSite(Integer id) {
        return removeById(id);
    }

    @Override
    public List<SiteNameVo> getSiteList() {
        LambdaQueryWrapper<Site> wrapper = new LambdaQueryWrapper<Site>()
                .select(Site::getId, Site::getSiteName);
        List<Site> siteList = list(wrapper);

        List<SiteNameVo> siteNameVoList = new ArrayList<>();
        if(siteList != null && !siteList.isEmpty()){
            for (Site site : siteList) {
                SiteNameVo siteNameVo = new SiteNameVo();
                BeanUtils.copyProperties(site, siteNameVo);
                siteNameVoList.add(siteNameVo);
            }
        }
        return siteNameVoList;
    }
}
